const MyPromise = require('./08-task-my-promise')

const p0 = new MyPromise((resolve, reject) => {
  resolve('p0 success')
  // reject('p0 error')
  // setTimeout(() => {
  //   // resolve('p0 success delay')
  //   reject('p0 error delay')
  // }, 2000);
})

const p = new MyPromise((resolve, reject) => {
  // resolve('p success')
  // reject('p error')
  setTimeout(() => {
    // resolve('p success delay')
    reject('p error delay')
  }, 2000);
})

p.then(value => {
  console.log(`成功1：${value}`)
  return 'p sucesss'
}).then(value => {
  console.log(`成功2：${value}`)
  return 'p sucesss'
}).then(value => {
  console.log(`成功3：${value}`)
}).then(undefined, err => {
  console.log(`catch error ${err}`)
})

// let p1 = p.then(value => {
//   console.log(`成功1：${value}`)
//   // throw new Error('p error')
//   return p1
// }, err => {
//   console.log(`失败1：${err}`)
// })

// p1.then(value => {
//   console.log(`成功1：${value}`)
// }, err => {
//   console.log(`失败1：${err}`)
// })

// p.finally(1).then(value => {
//   console.log(`成功2：${value}`)
//   return 'p sucesss'
// }, err => {
//   console.log(`失败2：${err}`)
// }).then(value => {
//   console.log(`成功3：${value}`)
// }, err => {
//   console.log(`失败3：${err}`)
// })


// function p1All () {
//   return new MyPromise((resolve, reject) => {
//     reject('ERROR p1All')
//   })
// }

// function p2All () {
//   return new MyPromise((resolve, reject) => {
//     setTimeout(() => {
//       resolve('p2All')
//     },0)
//   })
// }

// const pAll = MyPromise.all([1, p1All(), p2All(), 2, 3])
// pAll.then(value => {
//   console.log(value)
// }, err => {
//   console.log(`Error: ${err}`)
// })

// const pRace = MyPromise.race([p2All(), p1All()])
// pRace.then(value => {
//   console.log(value)
// }, err => {
//   console.log(`Error: ${err}`)
// })

// Promise.resolve(p).then(value => console.log(value))
// MyPromise.reject(p).then(value => console.log(value), err => console.log('1112' + err))
// 0 0.5 1 2 3 5 4 6 7
Promise.resolve().then(() => {
  console.log(0);
  return new Promise(resolve => {
    console.log(0.5)
    resolve(Promise.resolve(4));
  });
}).then((res) => {
  console.log(res)
}).then(() => {
  console.log(7)
})

Promise.resolve().then(() => {
  console.log(1);
}).then(() => {
  console.log(2);
}).then(() => {
  console.log(3);
}).then(() => {
  console.log(5);
}).then(() =>{
  console.log(6);
})

class Person {
  constructor (executor) {
    executor(this.solveName)
  }
  name = undefined
  isSet = 'ing'
  callback = []

  static setName (name) {
    return new Person(solveName => solveName(name))
  }

  solveName = name => {
    this.isSet = 'yes'
    this.name = name
    while (this.callback.length) this.callback.shift()()
  }

  say (callback) {
    return new Person(solveName => {
      if (this.isSet === 'yes') {
        setTimeout(() => {
          const name = callback(this.name)
          if (name instanceof Person) {
            name.say(solveName)
          } else {
            solveName(name)
          }
          solveName(name)
        }, 0);
      } else {
        this.callback.push(() => {
          setTimeout(() => {
            const name = callback(this.name)
            if (name instanceof Person) {
              name.say(solveName)
            } else {
              solveName(name)
            }
          }, 0);
        })
      }
    })
  }
}

// Person.setName().say(() => {
//   console.log('tom0')
//   return Person.setName('tom4')
//   // return new Person(solveName => solveName('tom4'))
// }).say((res) => {
//   console.log(res)
// }).say(() => {
//   console.log('tom4.5')
// })

// Person.setName().say(() => {
//   console.log('tom1')
// }).say(() => {
//   console.log('tom2')
// }).say(() => {
//   console.log('tom3')
// }).say(() => {
//   console.log('tom5')
// }).say(() => {
//   console.log('tom6')
// })
